package com.sl.mes.app.controller;


import com.alibaba.fastjson.JSONObject;
import com.sl.mes.core.util.JacksonUtil;
import com.sl.mes.core.util.ResponseUtil;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mokends
 * @create 2021-11-05  22:16
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/mes/order")
public class OrderController {

    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

    @RequestMapping(value = "/getOrderMessage", method = RequestMethod.GET)
    private List<Map<String, Object>> getOrderMessage() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", "0001");
        map.put("code", "66666666");
        map.put("oilType", "#93汽油");
        map.put("status", "库管审核通过");
        map.put("ts", "2021-08-23 13:07:55");

        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("id", "0002");
        map1.put("code", "777766");
        map1.put("oilType", "#95汽油");
        map1.put("status", "库管审核不通过");
        map1.put("ts", "2021-08-25 20:07:44");

        List<Map<String, Object>> orderMessagelist = new ArrayList<Map<String, Object>>();
        orderMessagelist.add(map);
        orderMessagelist.add(map1);
        return orderMessagelist;
    }

    @RequestMapping(value = "/getOrderPageInit", method = RequestMethod.GET)
    private Map<String, Object> getOilPageInit(String str) {
        HashMap<String, Object> data = new HashMap<>();

        //订单编号等信息
        HashMap<String, Object> orderMessage = new HashMap<>();
        orderMessage.put("id", "0001");
        orderMessage.put("code", "66666666");
        orderMessage.put("oilType", "#93汽油");
        orderMessage.put("ts", "2021-08-23 13:07:55");

        HashMap<String, Object> orderMessage1 = new HashMap<>();
        orderMessage1.put("id", "0002");
        orderMessage1.put("code", "666667777");
        orderMessage1.put("oilType", "#95汽油");
        orderMessage1.put("ts", "2021-08-23 13:07:55");


        //供应商信息
        HashMap<String, Object> supplierMessage1 = new HashMap<>();
        supplierMessage1.put("id","00001");
        supplierMessage1.put("name","中国石油物资有限公司");
        supplierMessage1.put("code","0000001");
        supplierMessage1.put("number",30000);
        HashMap<String, Object> supplierMessage2 = new HashMap<>();
        supplierMessage2.put("id","00002");
        supplierMessage2.put("name","科威特国家石油公司");
        supplierMessage2.put("code","0000002");
        supplierMessage2.put("number",40000);
        List<Map<String, Object>> supplierMessagelist = new ArrayList<Map<String, Object>>();
        supplierMessagelist.add(supplierMessage1);
        supplierMessagelist.add(supplierMessage2);

        //油库信息
        HashMap<String, Object> MyOilDepot1 = new HashMap<>();
        MyOilDepot1.put("id","00001");
        MyOilDepot1.put("name","玉溪");
        MyOilDepot1.put("code","000001");
        MyOilDepot1.put("total",20000);
        MyOilDepot1.put("available",10000);
        HashMap<String, Object> MyOilDepot2 = new HashMap<>();
        MyOilDepot2.put("id","00002");
        MyOilDepot2.put("name","大理");
        MyOilDepot2.put("code","0000002");
        MyOilDepot2.put("total",30000);
        MyOilDepot2.put("available",10000);
        List<Map<String, Object>> MyOilDepotlist = new ArrayList<Map<String, Object>>();
        MyOilDepotlist.add(MyOilDepot1);
        MyOilDepotlist.add(MyOilDepot2);


        data.put("orderMessage",orderMessage);
        data.put("supplierMessagelist",supplierMessagelist);
        data.put("MyOilDepotlist",MyOilDepotlist);
//        if(str =="001"){
//
//            data.put("orderMessage",orderMessage);
//            data.put("supplierMessagelist",supplierMessagelist);
//            data.put("MyOilDepotlist",MyOilDepotlist);
//
//
//        } else if (str == "002") {
//            data.put("orderMessage",orderMessage1);
//            data.put("supplierMessagelist",supplierMessagelist);
//            data.put("MyOilDepotlist",MyOilDepotlist);
//        }else{
//            data.put("err","没有找到订单信息");
//        }
        return data;
    }


    /**
     * 提交订单
     *
     * @param body
     *            请求内容，{ id: xxx, name: xxx,number:xxx }
     * @param request
     *            请求对象
     * @return 提交结果
     */
    @PostMapping("submitorder")
    public Object submitorder(@RequestBody String body, HttpServletRequest req, HttpServletResponse res) {

        logger.info("【请求开始】账户登录,请求参数,body:{}", body);

        String billstatus=JacksonUtil.parseString(body, "billstatus");
        String user=JacksonUtil.parseString(body, "user");
        String cusManager = JacksonUtil.parseString(body, "cusManager");
        String contactPsn =JacksonUtil.parseString(body, "contactPsn");
        String customer=JacksonUtil.parseString(body, "customer");
        String orderType=JacksonUtil.parseString(body, "orderType");
        String oilType=JacksonUtil.parseString(body, "oilType");

        List<HashMap<String, Object>> details=JacksonUtil.getJsonList(body,"details");

        // 第四步：转换POST表单Map并放入请求体
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        list.add(new BasicNameValuePair("transtype", "request_token"));
        list.add(new BasicNameValuePair("user_id", "test"));

        try {
            ApiRequestTools.exeDmobileRequest(req,res,list);
        } catch (Exception e){
            logger.info("Error", e.getMessage());

        }


//        if (username == null || password == null) {
//            return ResponseUtil.badArgument();
//        }

//        List<DtsUser> userList = userService.queryByUsername(username);
//        DtsUser user = null;
//        if (userList.size() > 1) {
//            logger.error("账户登录 出现多个同名用户错误,用户名:{},用户数量:{}", username, userList.size());
//            return ResponseUtil.serious();
//        } else if (userList.size() == 0) {
//            logger.error("账户登录 用户尚未存在,用户名:{}", username);
//            return ResponseUtil.badArgumentValue();
//        } else {
//            user = userList.get(0);
//        }

//        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
//        if (!encoder.matches(password, user.getPassword())) {
//            logger.error("账户登录 ,错误密码：{},{}", password, AUTH_INVALID_ACCOUNT.desc());// 错误的密码打印到日志中作为提示也无妨
//            return MesResponseUtil.fail(AUTH_INVALID_ACCOUNT);
//        }
//
//        // userInfo
//        UserInfo userInfo = new UserInfo();
//        userInfo.setNickName(username);
//        userInfo.setAvatarUrl(user.getAvatar());
//
//        try {
//            ZoneId zoneId = ZoneId.systemDefault();
//            LocalDateTime localDateTime = user.getAddTime() == null ?LocalDateTime.now(): user.getAddTime();
//            ZonedDateTime zdt = localDateTime.atZone(zoneId);
//            Date date = Date.from(zdt.toInstant());
//            String registerDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
//            userInfo.setRegisterDate(registerDate);
//            userInfo.setStatus(user.getStatus());
//            userInfo.setUserLevel(user.getUserLevel());// 用户层级
//            userInfo.setUserLevelDesc(UserTypeEnum.getInstance(user.getUserLevel()).getDesc());// 用户层级描述
//        } catch (Exception e) {
//            logger.error("账户登录：设置用户指定信息出错："+e.getMessage());
//            e.printStackTrace();
//        }
//
//        // token
//        UserToken userToken = null;
//        try {
//            userToken = UserTokenManager.generateToken(user.getId());
//        } catch (Exception e) {
//            logger.error("账户登录失败,生成token失败：{}", user.getId());
//            e.printStackTrace();
//            return ResponseUtil.fail();
//        }

        Map<Object, Object> result = new HashMap<Object, Object>();
//        result.put("token", userToken.getToken());
//        result.put("tokenExpire", userToken.getExpireTime().toString());
//        result.put("userInfo", userInfo);

        logger.info("【请求结束】账户登录,响应结果:{}", JSONObject.toJSONString(result));
        return ResponseUtil.ok(result);
    }

}
